package me.chan.graph;

import me.chan.std.In;

/**
 * Created by JanGin on 2020/4/26.
 */
public class Graph {

    private final int vetex;    //顶点数目

    private int edge;     //边的数目

    private Bag<Integer>[] adj;    //邻接表

    public Graph(int vetex) {
        this.vetex = vetex;
        this.adj = new Bag[vetex];
        this.edge = 0;
    }

    /**
     * 从标准输入中读取顶点
     * @param in
     */
    public Graph(In in) {
        this(in.readInt());
        int edge = in.readInt();
        for (int i =0; i < vetex; ++i) {
            int v = in.readInt();       //一个顶点
            int w = in.readInt();       //另一个顶点
            addEdge(v, w);

        }
    }

    public void addEdge(int vetex1, int vetex2) {
        adj[vetex1].add(vetex2);
        adj[vetex2].add(vetex1);
        ++edge;     //边数加一
    }

    public int getEdge() {
        return edge;
    }

    public int getVetex() {
        return vetex;
    }

    public Iterable<Integer> adj(int vetex) {
        return adj[vetex];
    }

}
